﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableControlFocusSwitch
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;


namespace DnProcessSimulateCommands;

public class CApCableControlFocusSwitch : IDisposable
{
  private readonly CUiFLEXCreationMode m_creationMode;
  private readonly ICApFLEXCableAttributeViewModel m_attributeViewModel;
  private readonly IEnumerable<ICApCableEditBoxControl> m_editBoxControls;
  private readonly ICApCableFocusableControl m_focusEscapeControl;

  public CApCableControlFocusSwitch(
    CUiFLEXCreationMode creationMode,
    ICApFLEXCableAttributeViewModel attributeViewModel,
    IEnumerable<ICApCableEditBoxControl> editBoxControls,
    ICApCableFocusableControl focusEscapeControl)
  {
    this.m_creationMode = creationMode;
    this.m_attributeViewModel = attributeViewModel ?? throw new ArgumentNullException(nameof (attributeViewModel));
    this.m_editBoxControls = (IEnumerable<ICApCableEditBoxControl>) ((editBoxControls != null ? editBoxControls.ToList<ICApCableEditBoxControl>() : (List<ICApCableEditBoxControl>) null) ?? throw new ArgumentNullException(nameof (editBoxControls)));
    this.m_focusEscapeControl = focusEscapeControl ?? throw new ArgumentNullException(nameof (focusEscapeControl));
    if (this.m_editBoxControls.Contains<ICApCableEditBoxControl>((ICApCableEditBoxControl) null))
      throw new ArgumentNullException("editBoxControl");
    this.RegisterToEvents();
  }

  public void Dispose() => this.UnregisterFromEvents();

  private void RegisterToEvents()
  {
    this.m_attributeViewModel.PropertyChanged += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
    foreach (ICApCableEditBoxControl editBoxControl in this.m_editBoxControls)
      editBoxControl.ObjectChanged += new CApCableEditBoxControlEventHandler(this.EditBoxControl_ObjectChanged);
  }

  private void UnregisterFromEvents()
  {
    this.m_attributeViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
    foreach (ICApCableEditBoxControl editBoxControl in this.m_editBoxControls)
      editBoxControl.ObjectChanged -= new CApCableEditBoxControlEventHandler(this.EditBoxControl_ObjectChanged);
  }

  private bool IsRelevantProperty(string propertyName)
  {
    return propertyName == "CreationMode" || propertyName == "IsParametersExpanded" || propertyName == "IsAttachmentExpanded";
  }

  private bool ShouldApplyFocusSwitch()
  {
    return this.m_creationMode == this.m_attributeViewModel.CreationMode && !this.m_attributeViewModel.IsBeingInitialized;
  }

  private bool ShouldApplyFocusSwitch(string propertyName)
  {
    return this.IsRelevantProperty(propertyName) && this.ShouldApplyFocusSwitch();
  }

  private bool ShouldApplyFocusSwitch(ICApCableEditBoxControl sender)
  {
    return sender?.DisplayableObject != null && this.ShouldApplyFocusSwitch();
  }

  private void ApplyFocusSwitch()
  {
    ICApCableEditBoxControl cableEditBoxControl = this.m_editBoxControls.FirstOrDefault<ICApCableEditBoxControl>((Func<ICApCableEditBoxControl, bool>) (e => e.DisplayableObject == null));
    if (cableEditBoxControl != null)
      cableEditBoxControl.SetFocus();
    else
      this.m_focusEscapeControl.SetFocus();
  }

  private void AttributeViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    if (!this.ShouldApplyFocusSwitch(e.PropertyName))
      return;
    this.ApplyFocusSwitch();
  }

  private void EditBoxControl_ObjectChanged(ICApCableEditBoxControl sender)
  {
    if (!this.ShouldApplyFocusSwitch(sender))
      return;
    this.ApplyFocusSwitch();
  }
}
